package com.example.client

/**
 * @Author: leali
 * @Version: 1.0
 * @Date: 9/16/2020-2:00 PM
 * @Description: Scala 自适应类型
 */
//noinspection ScalaDocUnknownTag,ScalaDocMissingParameterDescription
object MyClass {
  def printMethodInfo(methodName: String): Unit = {
    println(getClass.getDeclaredMethod(methodName, null).getReturnType.getName)
  }
  def method2(): Int = {
    6
  }

  def method3() = 6

  def method4(): Double = 6

  def method5 = 6

  class Pet(val name: String) {
    override def toString: String = name
  }

  class Dog(override val name: String) extends Pet(name)

  def workWithPets(pets: Array[Pet]): Unit = {}

  def workWithPets1[T <: Pet](pets: Array[T]): Unit = {
    println("call work: " + pets.mkString(","))
  }

  /**
   * 测试下界符
   *
   * @param fromPets
   * @param toPets
   * @tparam S Dog
   * @tparam D Pet
   */
  def copyPets[S, D >: S](fromPets: Array[S], toPets: Array[D]): Unit = {

  }

  /**
   * 模式匹配处理
   *
   * @param input
   */
  def process(input: String): Unit = {
    val MatchStock = """^(.+):(\d*\.\d+)""".r
    input match {
      case MatchStock("APPLE", price) => println("Apple is Very good on " + price)
      case MatchStock("IBM", price) => println("IBM's trading at " + price)
      case MatchStock(symbol, price) => printf("Price of %s is %s\n", symbol, price)
      case _ => println("not processing: " + input)
    }
  }

  def testMatch(args:Any*): Unit ={
    for(arg<-args){
      arg match {
        case s:String => println("string:",s)
        case i:Int => println("int:",i)
        case d:Double => println("double:",d)
        case  _ => println("args is error: ",arg)
      }
    }
  }
  def main(args: Array[String]): Unit = {
    //      printMethodInfo("method1")
    //      printMethodInfo("method2")
    //      printMethodInfo("method3")
    //      printMethodInfo("method4")
    //      printMethodInfo("method5")

    // 测试上界类型
    //    val dogs = Array(new Dog("Rover"), new Dog("Comet"))
    //    //workWithPets(dogs) // type mismatch;
    //    workWithPets1(dogs)
    process("APPLE:234.34")
    process("IBM:346.42")
    process("TES:452.45")
    process("DFGD:23")
    testMatch(1,"2342",23.43)
  }

}
